package cn.com.recode.b1找出泥团函数提取;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {

    private String _name;

    private Vector _rentals = new Vector();

    public Customer(String name) {
        this._name = name;
    }

    public void addRental(Rental rental) {
        _rentals.add(rental);
    }

    public String getName() {
        return _name;
    }

    /**
     * 1.找出代码的泥团, switch语句
     * 2. 任何不会被修改的变量可以被当成参数传入新的函数，会被修改的变量需要格外小心
     * 3.变量名称规范化，可通过名字即可知道用意
     * @return
     */
    public String statement() {
        double totalAmout = 0;
        int frenquentRenterPoints = 0;
        Enumeration rentals = _rentals.elements();
        String result = "Rental  Record for " + getName() + "\n";
        while (rentals.hasMoreElements()) {
            double thisAmout = 0;
            Rental each = (Rental) rentals.nextElement();

            thisAmout = amountFor(each);

            frenquentRenterPoints ++;
            if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) {
                frenquentRenterPoints ++;
            }

            result += "\t" +each.getMovie().getTitle()+"\t"+
                    String.valueOf(thisAmout) + "\n";

            totalAmout += thisAmout;
        }

        result += "Amout owed is "+String.valueOf(totalAmout) +"\n";
        result += "You earned " + String.valueOf(frenquentRenterPoints)+" frequent renter points";
        return result;
    }

    private double amountFor(Rental rental) {
        double thisAmout = 0;
        switch (rental.getMovie().getPriceCode()) {
            case Movie.REGULAR:
                thisAmout += 2;
                if (rental.getDaysRented() > 2) {
                    thisAmout += (rental.getDaysRented() - 2) * 1.5;
                }
                break;
            case Movie.NEW_RELEASE:
                thisAmout += rental.getDaysRented() * 3;
                break;
            case Movie.CHILDRENS:
                thisAmout += 1.5;
                if (rental.getDaysRented() > 3) {
                    thisAmout += (rental.getDaysRented() - 3) * 1.5;
                }
                break;
        }
        return thisAmout;
    }
}
